/*** manager ***/
package util;

import java.util.GregorianCalendar;

public class Calendar {

  public static final int PREVIOUS_MONTH = -1;
  public static final int TODAY = 0;
  public static final int NEXT_MONTH = 1;
  

  private static String[][] calendar = new String[6][7];
  public static final String[] months = {
    "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho",
    "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
  public static final String[] days = {"Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sab"};
  private static int today,  year,  month;

  private static void clear() {
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 7; j++) {
        calendar[i][j] = "";
      }
    }
  }

  private static void fill() {

    //Get first day of month and number of days
    GregorianCalendar cal = new GregorianCalendar(year, month, 1);
    int nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
    int som = cal.get(GregorianCalendar.DAY_OF_WEEK);

    //Draw calendar
    for (int i = 1; i <= nod; i++) {
      int row = new Integer((i + som - 2) / 7);
      int column = (i + som - 2) % 7;
      calendar[row][column] = String.valueOf(i);
    }

  }

  private static void setToday() {
    GregorianCalendar todayCal = new GregorianCalendar();
    today = todayCal.get(GregorianCalendar.DAY_OF_MONTH);
    month = todayCal.get(GregorianCalendar.MONTH);
    year = todayCal.get(GregorianCalendar.YEAR);
  }

  /**
   * Get the current date
   * @return a tuple with three ints: year, month and today
   */
  public static int[] getToday() {
    GregorianCalendar todayCal = new GregorianCalendar();
    return new int[]{
              todayCal.get(GregorianCalendar.YEAR),
              todayCal.get(GregorianCalendar.MONTH),
              todayCal.get(GregorianCalendar.DAY_OF_MONTH)};
  }

  public static int[] getTodayPosition() {
    GregorianCalendar todayCal = new GregorianCalendar();
    if ((todayCal.get(GregorianCalendar.YEAR) == year) && (todayCal.get(GregorianCalendar.MONTH) == month)) {
      for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 7; j++) {
          if (calendar[i][j].equals(String.valueOf(today))) {
            return new int[]{i, j};
          }
        }
      }
    }
    return new int[]{-1, -1};
  }

  public static String getMonthName() {
    return months[month];
  }

  public static String getMonthAndYear() {
    return months[month] + " de " + year;
  }

  public static String[][] getTodayCalendar() {
    setToday();
    return getCalendar();
  }

  public static String[][] getCalendarNextMonth() {
    if (++Calendar.month == 12) {
      Calendar.month = 0;
      Calendar.year += 1;
    }

    return getCalendar();
  }

  public static String[][] getCalendarPreviousMonth() {
    if (--Calendar.month == -1) {
      Calendar.month = 11;
      Calendar.year -= 1;
    }
    return getCalendar();
  }

  public static String[][] getCalendar(int year, int month) {
    Calendar.year = year;
    Calendar.month = month;
    return getCalendar();
  }

  public static String[][] getCalendar() {
    clear();
    fill();
    return calendar;
  }
}
